$(document).ready(function(){
   $('.rep_innerwrap.contrast .datebar:visible').each(function(){
      var datebar = $(this);
      $(this).dateSelect({
         defaultType: 'month',
         contrast: true,
         onFinished: function(){
            $('a.startcollect', datebar).trigger('click');
         }
      });
   });

   $('.rep_innerwrap.contrast a[data-toggle="tab"]').on('shown', function (e) {
      var type = $(this).data('type');
      var target = $($(this).attr('href'));
      var container = target.find('.content');
      if(type == 'source' && !container.html()){
         var datebar = target.find('.datebar');
         datebar.dateSelect({
            defaultType: 'custom',
            onFinished: function(){
               $('a.startcollect', datebar).trigger('click');
            }
         });
      };
   });

   $('.tab-pane').on('click', '.sourceSel', function(){
      var form = $(this).closest('form');
      var start = $('.start', form).val(), end = $('.end', form).val();
      if(start > end){
         showMessagePanel('开始时间不能大于结束时间', $('a.startcollect', form), true, {placement:'right'});
         return false;
      }

      if ($(this).prop('checked')) {
         $(this).closest(".checkbox").addClass("checkedbox");
      } else {
         $(this).closest(".checkbox").removeClass("checkedbox");
      }

      if ($('.sourceSel:checked').length <=0) {
         $('.noItemTips').show().siblings('.sourceContrastBars').html('').hide();
         return true;
      } else if($('.sourceSel:checked').length <=1) {
         $('.noItemTips').hide().siblings('.sourceContrastBars').show();
      }

      var target = $(this);
      form.ajaxSubmit({
         data: {showLabel: 0},
         beforeSubmit: function(data, form, options) {
            if (!showLoading(target,null,null,{button:true}))
               return false;
         },
         success: function(result, status, xhr, form) {
            $('.sourceContrastBars').html(result);
         }
      });
      return true;
   });
});